<template>
	<div class="role" v-if="dialogFormVisible">
		<el-dialog :title="title" :visible="dialogFormVisible" :close-on-click-modal="false" width="800px" :before-close="handleClose">
			<div class="tab">
				<el-form ref="form" :rules="rules" :model="form" label-width="80px">
					<el-form-item label="组名" prop="name">
						<el-input v-model="form.name" placeholder="请输入组名" :disabled="num == 3"></el-input>
					</el-form-item>
					<el-form-item label="状态">
						<el-select v-model="form.status" placeholder="请选择状态" style="width:100%;" :disabled="num == 3">
							<el-option label="禁用" :value="0"></el-option>
							<el-option label="正常" :value="1"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="产品线" v-if="num == 1">
						<el-select
							v-model="form.product_line"
							multiple
							placeholder="请选择"
							style="width:100%;"
							:disabled="num == 3">
							<el-option
								v-for="item in optionsline"
								:key="item.id"
								:label="item.name"
								:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="产品线" v-if="num == 3">
						<el-select
							v-model="lineId"
							multiple
							placeholder="请选择"
							style="width:100%;"
							:disabled="num == 3">
							<el-option
								v-for="item in optionsline"
								:key="item.id"
								:label="item.name"
								:value="item.name">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="组长">
						<el-select
							v-model="userLeader"
							multiple
							placeholder="请选择"
							style="width:100%;"
							:disabled="num == 3">
							<el-option
								v-for="item in optionsLeader"
								:key="item.id"
								:label="item.name"
								:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="组员">
						<el-select
							v-model="userSpare"
							multiple
							placeholder="请选择"
							style="width:100%;"
							:disabled="num == 3">
							<el-option
								v-for="item in options"
								:key="item.id"
								:label="item.name"
								:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="描述" prop="desc">
						<el-input type="textarea" placeholder="请输入描述" v-model="form.desc" :disabled="num == 3"></el-input>
					</el-form-item>
				</el-form>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="handleClose">取 消</el-button>
				<el-button type="primary" @click="add('form')" v-if="num != 3">提 交</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { getHeader } from '@/app.config'
export default {
	name: 'addForm',
	data(){
		return {
			dialogFormVisible:false,
			form:{
				name:'',
				status:1
			},
			id:'',
			rules:{
				name: [
					{max: 30, message: '组名不能超过30个字符', trigger: 'blur'},
					{ required: true, message: '请输入组名', trigger: 'blur' }
				],
				desc:[
					{max: 200, message: '描述不能超过200个字符', trigger: 'blur'},
				]
			},
			options:{},
			optionsLeader:{},
			num:0,
			title:'新增',
			userLeader:[],
			userSpare:[],
			optionsline:[],
			lineId:[]
		}
	},
	created() {

	},
	methods:{
		handleClose(){
			this.dialogFormVisible = false
			this.form = {}
			this.userLeader = []
			this.userSpare = []
		},
		add(form){
			this.$refs[form].validate((valid) => {
				if (valid) {
					if(this.num == 1){
						var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/group`;
						var data = this.form
						this.form.user = this.userLeader.concat(this.userSpare)
						console.log(data,2121)
						this.$axios.post(source,data,{headers: getHeader()})
							.then((response) => {
								if (response.status == 200) {
									this.dialogFormVisible = false
									this.form = {}
									this.$parent.initTable()
									this.$message.success("新增成功!")
									this.userLeader = []
									this.userSpare = []
								}
							}).catch((err) => {
							console.log(err);
						});
					}

				} else {
					return false;
				}
			});

		},
		getOption(){
			var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/user/list?role=${'test'}`;
			this.$axios.get(source,{headers: getHeader()})
				.then((response) => {
					if (response.status == 200) {
						this.options = Object.assign({},response.data.data)
					}
				}).catch((err) => {
				console.log(err);
			});

			var source1 = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/user/list?role=${'test_leader'}`;
			this.$axios.get(source1,{headers: getHeader()})
				.then((response) => {
					if (response.status == 200) {
						this.optionsLeader = Object.assign({},response.data.data)
					}
				}).catch((err) => {
				console.log(err);
			});

			var source2 = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/product/line/list`;
			this.$axios.get(source2,{headers: getHeader()})
				.then((response) => {
					if (response.status == 200) {
						console.log(response.data.data,23232)
						this.optionsline = response.data.data
					}
				}).catch((err) => {
				console.log(err);
			});
		},
		initData(num,data){
			this.id = data
			this.num = num
			if(num == 1){
				this.title = '新增'
				this.getOption()
			}

			if(num == 3){
				this.title = '查看'
				var source = `${process.env.VUE_APP_KANBAN_HOST}/api/v1/group/${data}`;
				this.$axios.get(source,{headers: getHeader()})
					.then((response) => {
						if (response.status == 200) {
							var res = response.data.data
							this.form = Object.assign({},res)
							this.form.test_leader = []
							this.form.tester = []
							res.test_leader.filter(item => {
								this.userLeader.push(item.name)
							})
							res.tester.filter(item => {
								this.userSpare.push(item.name)
							})
							res.product_line.filter(item => {
								this.lineId.push(item['name'])
							})
						}
					}).catch((err) => {
					console.log(err);
				});
			}
		}
	}

}
</script>

<style lang="scss" scoped>
.role{
	//.tab{
	//	width: 450px;
	//	margin: 0 0 0 40px;
	//}
}
</style>
